package service

import (
	"context"
	"fmt"
	__ "user-srv/basic/proto"
	"user-srv/handler/dao"
)

// 用户注册
func (s *Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {
	if in.Mobile == "" || in.Password == "" || in.Nickname == "" {
		return nil, fmt.Errorf("参数不能为空")
	}

	register, err := dao.Register(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.CreateUserResp{
		Id: int32(register.Id),
	}, nil
}

// // 用户登录
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	if in.Mobile == "" || in.Password == "" {
		return nil, fmt.Errorf("参数不能为空")
	}

	user, err := dao.Login(in)
	if err != nil {
		return nil, err
	}

	return &__.LoginResp{
		Id:       int64(user.Id),
		Mobile:   user.Mobile,
		Password: user.Password,
		Nickname: user.Nickname,
		Status:   int64(user.Status),
	}, nil
}

// 用户注销
func (s *Server) UserDel(_ context.Context, in *__.UserDelReq) (*__.UserDelResp, error) {
	if in.Id <= 0 {
		return nil, fmt.Errorf("参数错误")
	}
	_, err := dao.UserDel(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.UserDelResp{}, nil
}

// 用户修改密码
func (s *Server) UserUpdatePassword(_ context.Context, in *__.UserUpdatePasswordReq) (*__.UserUpdatePasswordResp, error) {
	if in.Password == "" || in.RePassword == "" || in.NewPassword == "" {
		return nil, fmt.Errorf("参数不能为空")
	}
	password, err := dao.UserUpdatePassword(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.UserUpdatePasswordResp{
		Id: int32(password.Id),
	}, nil
}
